<html>
<head><meta charset="utf-8"><title>impl From&lt;$int&gt; for RangeInclusive&lt;$int&gt; · t-libs · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/index.html">t-libs</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html">impl From&lt;$int&gt; for RangeInclusive&lt;$int&gt;</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205281511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281511">(Jul 28 2020 at 19:44)</a>:</h4>
<p>Hello everyone!<br>
Is there a reason <code>impl From&lt;$int&gt; for RangeInclusive&lt;$int&gt;</code> is not implemented in the standard library? Would it probably be accepted if I added such an impl?</p>



<a name="205281596"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281596" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281596">(Jul 28 2020 at 19:45)</a>:</h4>
<p>do you also want to implement <code>impl From&lt;$int&gt; for RangeFrom&lt;$int&gt;</code>?</p>



<a name="205281724"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281724" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281724">(Jul 28 2020 at 19:46)</a>:</h4>
<p>Well, I currently don't need it, but if it makes sense, I can group both impl in the same tracking issue / PR.</p>



<a name="205281820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281820">(Jul 28 2020 at 19:47)</a>:</h4>
<p>imo the latter doesn't really make sense</p>



<a name="205281844"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281844" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281844">(Jul 28 2020 at 19:47)</a>:</h4>
<p>turning an int into a range that contains just that int seems fine though</p>



<a name="205281930"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281930" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281930">(Jul 28 2020 at 19:48)</a>:</h4>
<p>wait, so RangeInclusive impls would be <code>x -&gt; x..=x</code>?</p>



<a name="205281948"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205281948" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205281948">(Jul 28 2020 at 19:48)</a>:</h4>
<p>Yes</p>



<a name="205282084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205282084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205282084">(Jul 28 2020 at 19:49)</a>:</h4>
<p>hmm, that seems fine for me, I somehow thought <code>RangeInclusive</code> was <code>x -&gt; ..=x</code> which I don't want (edit: I actually thought of something like <code>0..=x</code>, but w/e)</p>



<a name="205282136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205282136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205282136">(Jul 28 2020 at 19:49)</a>:</h4>
<p>I know its not terribly useful i guess, but in my case, I have some macros that converts some type to some other type using the From trait and I have to wrap RangeInclusive to be able to implement the From trait</p>



<a name="205334695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205334695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205334695">(Jul 29 2020 at 09:31)</a>:</h4>
<p>It’s not obvious to me that this conversion <em>should</em> exist in a general purpose trait</p>



<a name="205334783"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205334783" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Simon Sapin <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205334783">(Jul 29 2020 at 09:32)</a>:</h4>
<p>In some sense <code>4_u8</code> is/means "the same thing" as <code>4_u32</code> so that conversion should exist, but that’s not the case here</p>



<a name="205503080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205503080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pickfire <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205503080">(Jul 30 2020 at 16:48)</a>:</h4>
<p>Sounds not so good to me but I think it may confuse other to think that <code>x</code> means <code>0..=x</code> rather than <code>x..=x</code>. Potential footgun, but I think most cases they may rather implement <code>Index&lt;usize&gt;</code> when they need <code>x</code> to be <code>x..=x</code> for <code>Index&lt;RangeInclusive&lt;usize&gt;&gt;</code>. Not sure where others still need <code>RangeInclusive&lt;$num&gt;</code>. <span class="user-mention" data-user-id="281572">@marmeladema</span> Do you have a use case for this?</p>



<a name="205512203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205512203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205512203">(Jul 30 2020 at 18:08)</a>:</h4>
<p>i feel like <code>x..=x</code> is simpler to type out than trying to remember what a particular From impl thinks it should be doing.</p>



<a name="205668127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205668127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205668127">(Aug 01 2020 at 06:34)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> Maybe experiment with a <code>RangeInclusive::single(x)</code> method? That way it's not insta-stable, so has a lower bar.  And with <code>Step</code>, <code>Range::single(x)</code> could be made to work too...</p>



<a name="205673052"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/219381-t-libs/topic/impl%20From%3C%24int%3E%20for%20RangeInclusive%3C%24int%3E/near/205673052" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/219381-t-libs/topic/impl.20From.3C.24int.3E.20for.20RangeInclusive.3C.24int.3E.html#205673052">(Aug 01 2020 at 09:14)</a>:</h4>
<p>That would not really help me as I am dealing with a macro that converts an enum which olds different kind of data into another enum which holds "ranges" of that data using the <code>Into</code> trait. It's not a big issue, i wrapped <code>InclusiveRange</code> into a new type and everyone is happy.</p>
<p>Since people in this thread seem to be confused about such an <code>Impl</code> being the standard library, I won't bother proposing this change but thank you everyone for your feedbacks <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>